package persistencia;

import java.util.Properties;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;

/**
 * Esta clase realiza la <strong>conexion</strong> a la Base de Datos mediante
 * la fabrica PersistenceManager. Esta clase esta implementada mediante el uso
 * del patron de diseno Singleton
 * 
 * @see javax.jdo.JDOHelper;
 * @see javax.jdo.PersistenceManagerFactory;
 * 
 * 
 * @version 1.0 --- 2011
 * 
 */
public class ConexionDB {
	private static PersistenceManagerFactory pmfinstance;

	/**
	 * Retorna la conexion a la base de datos obtenida por la
	 * PersistenceManagerFactory.
	 * 
	 * @see javax.jdo.PersistencemanagerFactory
	 * 
	 * @return static PersistenceManagerFactory
	 */
	public static PersistenceManagerFactory getConexion() {
		if (pmfinstance == null){
			String pmfClass = "com.triactive.jdo.PersistenceManagerFactoryImpl";
		    String driver = "org.gjt.mm.mysql.Driver";
		    String url = "jdbc:mysql://localhost/abmjsp";
		    String userName = "root";
		    String password = "root";
		    String autoCreateTable = "true";
			
		    Properties props = new Properties();

		    // Set the PersistenceManagerFactoryClass to the TJDO class.
		    props.setProperty("javax.jdo.PersistenceManagerFactoryClass",
		                      pmfClass);
		    
		    
		


		    // Set the JDBC driver name.  We'll use Cloudscape 4.0.
		    props.setProperty("javax.jdo.option.ConnectionDriverName",
		                      driver);

		    // Set the connection URL for the Cloudscape database.
		    props.setProperty("javax.jdo.option.ConnectionURL",
		                      url);
		    
		    


		    props.setProperty("javax.jdo.option.ConnectionUserName", userName);

		    props.setProperty("javax.jdo.option.ConnectionPassword", password);

		    props.setProperty("com.triactive.jdo.autoCreateTables", autoCreateTable);

		    // Get an instance of the PersistenceManagerFactory via JDOHelper.
		    pmfinstance = JDOHelper.getPersistenceManagerFactory(props);

		}
		return pmfinstance;
	}
	
	public static void cerrarConexion(){
		pmfinstance.close();
		System.out.println("CONEXION CERRADA");
	}
}
